package com.dsx.test.async.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author : tianwenqing
 * @version : 1.0
 * @date : 2019-09-26 20:31
 * @description :
 **/

@Configuration
public class AsyncConfig {


    /**
     * the core pool size of thread pool
     */
    @Value("${system.async.core-pool-size:2}")
    private int corePoolSize;
    /**
     * the max pool size of thread pool
     */
    @Value("${system.async.max-pool-size:10}")
    private int maxPoolSize;

    /**
     * the  thread pool name
     */
    @Value("${system.async.thread-name:api}")
    private String threadName;


    /**
     * the  queue size of thread pool
     */
    @Value("${system.async.queue-size:3}")
    private int queueSize;

    /**
     * 注入 Spring 线程池
     *
     * @return
     */
    @Bean
    public TaskExecutor taskExecutor() {

        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setThreadNamePrefix(threadName);
        taskExecutor.setCorePoolSize(corePoolSize);
        taskExecutor.setMaxPoolSize(maxPoolSize);
        taskExecutor.setQueueCapacity(queueSize);
        return taskExecutor;
    }
}
